using System;
using System.Web;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Security;
using THANHTUNGO.Portal.Components.Settings;
using THANHTUNGO.Portal.Components.UI;
using Page=THANHTUNGO.Portal.Components.UI.Page;

namespace THANHTUNGO.Portal
{
    public partial class Default : Page
    {
        protected override void OnInit(EventArgs e)
        {
            // Obtain PortalSettings from Current Context
            PortalSettings portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
            LeftPaneBorder.Visible = LeftPane.Visible = portalSettings.ActiveTab.ShowLeftPane;
            RightPaneBorder.Visible = RightPane.Visible = portalSettings.ActiveTab.ShowRightPane;

            if (Request.QueryString["cid"] != null)
            {
                ContentPane.Controls.Add(
                    Page.LoadControl(string.Format("~/Controls/{0}.ascx", Request.QueryString["cid"])));
            }
            else
            {
                // Ensure that the visiting user has access to the current page
                if (PortalSecurity.IsInRoles(portalSettings.ActiveTab.AuthorizedRoles) == false)
                {
                    Response.Redirect("~/Errors/AccessDenied.aspx");
                }

                // Dynamically Populate the Left, Center and Right pane sections of the portal page
                if (portalSettings.ActiveTab.Modules.Count > 0)
                {
                    // Loop through each entry in the configuration system for this tab
                    foreach (Module module in portalSettings.ActiveTab.Modules)
                    {
                        Control parent;
                        switch (module.PaneName)
                        {
                            case "LeftPane":
                                parent = LeftPane;
                                break;
                            case "RightPane":
                                parent = RightPane;
                                break;
                            default:
                                parent = ContentPane;
                                break;
                        }

                        // If no caching is specified, create the user control instance and dynamically
                        // inject it into the page.  Otherwise, create a cached module instance that
                        // may or may not optionally inject the module into the tree

                        bool isViewableModule = PortalSecurity.HasViewPermission(module.ID) ||
                                                PortalSecurity.IsInRole("1");
                        if (module.CacheTimeOut == 0)
                        {
                            // thanhtung: Kiem tra xem co quyen thay module khong?
                            if (isViewableModule)
                            {
                                PortalModuleControl portalModule =
                                    (PortalModuleControl) Page.LoadControl(module.SourceFile);
                                portalModule.PortalID = portalSettings.ID;
                                portalModule.Module = module;
                                parent.Controls.Add(portalModule);
                            }
                        }
                        else
                        {
                            CachedPortalModuleControl portalModule = new CachedPortalModuleControl();
                            portalModule.PortalId = portalSettings.ID;
                            portalModule.ModuleConfiguration = module;
                            parent.Controls.Add(portalModule);
                        }
                        parent.Visible = true;
                    }
                }
            }
            base.OnInit(e);
        }
    }
}